﻿@using CommonLibrary.Models
@{    
    wf_Table table;
    List<wf_Column> columns;
    var field = (wf_FieldInstance)this.Model;
    using (CommonDatabase db = new CommonDatabase())
    {
        table = db.wf_Table.Where(k => k.Id == field.Source).First();
        columns = db.wf_Column.Where(k => k.TableId == table.Id).ToList();
    }

    var div = "div_" + table.Id;
    var grid = "grid_" + table.Id;
    var dlgAdd = "dlgAdd_" + table.Id;
    var dlgUpdate = "dlgUpdate_" + table.Id;
    var formAdd = "formAdd_" + table.Id;
    var formUpdate = "formUpdate_" + table.Id;
    var sb = new System.Text.StringBuilder();
}

<div id="@div">
    <div id="@grid"></div>
    <input id="field_@field.Id" name="field_@field.Id" type="hidden" />
</div>

<script type="text/javascript">
    $(document).ready(function () {
        var control = {};
        function change() {
            var grid = liger.get('@grid');
            var data = grid.getData();
            $('#field_@field.Id').val(liger.toJSON(data));
        }

        function add_click() {
            var formAdd = liger.get('@formAdd');
            if (formAdd.valid()) {
                var grid = liger.get('@grid');
                var d = formAdd.getData();
                var t = {};
                for(var k in d)
                {
                    if(d.hasOwnProperty(k)){
                        t[k] = d[k];
                    }
                }

                grid.add(t);
                change();
                control['@dlgAdd'].hide();
            }
            else {
                formAdd.showInvalid();
            }
        }

        function update_click() {
            var formUpdate = liger.get('@formUpdate');
            if (formUpdate.valid()) {
                var dlg = control['@dlgUpdate'];
                var grid = liger.get('@grid');
                formUpdate.getData();
                change();
                grid.reRender();
                dlg.hide();
            }
            else {
                formUpdate.showInvalid();
            }
        }

        function del(row) {
            liger.get('@grid').remove(row);
            change();
        }

        function openDialog(id, title, callback) {
            var dialog = control[id];
            if (dialog) {
                dialog.show();
            } else {
                control[id] = dialog = $.ligerDialog.open({
                    target: $('#' + id),
                    width: 600,
                    height: 400,
                    title: title,
                    isResize: true,
                    modal: true,
                    buttons: [
                        {
                            text: "确定", onclick: callback
                        },
                        {
                            text: "取消", onclick: function () {
                                dialog.hide();
                            }
                        }
                    ]
                });
            }

            return dialog;
        }

        function click_event(item) {
            switch (item.method) {
                case 'add':
                    openDialog('@dlgAdd', '添加', add_click);
                    break;

                case 'update':
                    DoAction(liger.get('@grid'), function (row) {
                        var dialog = openDialog('@dlgUpdate', '修改', update_click);
                        dialog.row = row;
                        liger.get('@formUpdate').setData(row);
                    }, '请选择需要修改的行');

                    break;

                case 'del':
                    DoActionConfirm(liger.get('@grid'), del, '请选择需要删除的行。', '是否删除当前选择行？');
                    break;
            }
        }

        $('#tt-continue').append(
$('<div id="@dlgAdd" style="display:none;padding-left:60px;">' +
    '<form id="@formAdd"></form>' +
'</div>' +
'<div id="@dlgUpdate" style="display:none;padding-left:60px;">' +
    '<form id="@formUpdate"></form>' +
'</div>'));

        $('#@grid').ligerGrid({
            resizable: false,
            colDraggable: false,
            dataAction: 'local',
            width: 'auto',
            height: 250,
            celldiff: -36,
            enabledSort: false,
            toolbar: {
                items: [
                    { method: 'add', click: click_event, text: '增加', icon: 'add' }
                    , { line: true }
                    , { method: 'update', click: click_event, text: '修改', icon: 'modify' }
                    , { line: true }
                    , { method: 'del', click: click_event, text: '删除', icon: 'delete' }
                ]
            },
            columns: [
                @for(int i = 0; i < columns.Count; i++)
                {
                    var type = "text";
                    var attr = "";
                    var item = columns[i];
                    switch (item.ControlId)
                    {              
                        case 3:
                            type = "date";
                            break;
                                          
                        case 5:
                            type = "int";
                            attr = ",totalSummary:{type:'sum'}";
                            break;

                        case 6:
                            type = "float";
                            attr = ",totalSummary:{type:'sum'}";
                            break;
                    }
                    
                    if(i == 0){
                        sb.AppendFormat("{{display:'{0}',width:120,name:'column_{1}',type:'{2}'{3}}}",
                            item.ColumnName, item.Id, type, attr);
                    }
                    else{
                        sb.AppendFormat(",{{display:'{0}',width:120,name:'column_{1}',type:'{2}'{3}}}",
                            item.ColumnName, item.Id, type, attr);
                    }
                }
                @Html.Raw(sb.ToString())
            ]
        });

        var options = {
            prefixID: '@formAdd',
            inputWidth: 170,
            labelWidth: 90,
            space: 40,
            validate: {},
            fields: [
                /* 表单字段 */
                @{ sb.Clear(); }
                @for(int i = 0; i < columns.Count; i++)
                {                   
                    var type = "textbox";
                    var attr = "";
                    var item = columns[i];
                    switch (item.ControlId)
                    {
                        case 1:
                            type = "textbox";
                            break;
                            
                        case 2:
                            type = "textarea";
                            break;
                            
                        case 3:
                            type = "date";
                            break;
                        
                        case 4:
                            type = "select";
                            break;
                            
                        case 5:
                            type = "digits";
                            break;

                        case 6:
                            type = "number";
                            break;
                            
                        case 7:
                            type = "hidden";
                            break;
                    }
                    
                    if(!item.Nullable)
                    {
                        attr = ",validate:{required:true}";
                    }
                    
                    if(i == 0)
                    {
                        sb.AppendFormat("{{label:'{0}',newline:true,name:'column_{1}',type:'{2}'{3}}}",
                            item.ColumnName, item.Id, type, attr);
                    }
                    else
                    {
                        sb.AppendFormat(",{{label:'{0}',newline:120,name:'column_{1}',type:'{2}'{3}}}",
                            item.ColumnName, item.Id, type, attr);
                    }
                }
                @Html.Raw(sb.ToString())
            ]
        };

        $('#@formAdd').ligerForm(options);

        var options2 = options;
        options2.prefixID = '@formUpdate';
        $('#@formUpdate').ligerForm(options2);
    });
</script>